package org.ymx.myArrayList;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class MyArrayList<AnyType> implements Iterable<AnyType> {

    private static final int DEFAULT_CAPACITY = 10;

    private Integer size;

    private AnyType[] items;

    /**
     * 构造方法
     */
    public MyArrayList() {
        doClean();
    }

    /**
     * 有参构造
     *
     * @param initCapacity
     */
    public MyArrayList(int initCapacity) {
        if (initCapacity > 0) {
            this.size = initCapacity;
        } else {
            throw new IllegalArgumentException();
        }
    }

    /**
     * 清空元素
     */
    public void clear() {
        doClean();
    }

    /**
     * 判断是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 删除多余长度
     */
    public void trimToSize() {
        ensureCapacity(size());
    }

    /**
     * 大小
     *
     * @return
     */
    public int size() {
        return size;
    }

    /**
     * 清空
     */
    public void doClean() {
        size = 0;
        ensureCapacity(DEFAULT_CAPACITY);
    }


    /**
     * 添加元素有目标位置的添加
     *
     * @param index
     * @param item
     */
    public void add(int index, AnyType item) {
        if (items.length == size()) {
            ensureCapacity(size() * 2 + 1);
        }
        for (int i = size; i > index; i--) {
            items[i] = items[i - 1];
        }
        items[index] = item;
        size++;
    }

    /**
     * 添加元素 无目标位置
     *
     * @param item
     */
    public void add(AnyType item) {
        add(size, item);
    }


    /**
     * 根据下标获取一个元素
     *
     * @param index
     * @return
     */
    public AnyType get(int index) {
        if (index >= size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return items[index];
    }

    /**
     * 替换一个元素
     *
     * @param index
     * @param item
     * @return
     */
    public AnyType set(int index, AnyType item) {
        if (index >= size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        AnyType old = items[index];
        items[index] = item;
        return old;
    }

    /**
     * 扩容
     *
     * @param newCapacity
     */
    public void ensureCapacity(int newCapacity) {
        if (newCapacity < size) {
            return;
        }
        AnyType[] old = items;
        AnyType[] newItems = (AnyType[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newItems[i] = old[i];
        }
        items = newItems;
    }

    /**
     * 删除一个元素
     *
     * @param index
     * @return
     */
    public AnyType remove(int index) {
        if (index >= size()) {
            throw new IndexOutOfBoundsException();
        }
        AnyType item = items[index];
        for (int i = index; i < size() - 1; i++) {
            items[i] = items[i + 1];
        }
        size--;
        return item;
    }

    @Override
    public Iterator<AnyType> iterator() {
        return new MyArrayListIterator();
    }

    /**
     * 内部迭代器的实现
     */
    private class MyArrayListIterator implements Iterator<AnyType> {

        private int current = 0;

        @Override
        public boolean hasNext() {
            return current < size();
        }

        @Override
        public AnyType next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return items[current++];
        }

        @Override
        public void remove() {
            MyArrayList.this.remove(--current);
        }
    }
}
